/**
 * Created by Administrator on 17-6-19.
 */
var conn = {};
conn = new WebIM.connection({
    isMultiLoginSessions: WebIM.config.isMultiLoginSessions,
    https: typeof WebIM.config.https === 'boolean' ? WebIM.config.https : location.protocol === 'https:',
    url: WebIM.config.xmppURL,
    isAutoLogin: true,
    heartBeatWait: WebIM.config.heartBeatWait,
    autoReconnectNumMax: WebIM.config.autoReconnectNumMax,
    autoReconnectInterval: WebIM.config.autoReconnectInterval
});
$(function(){
    var name="s051001";

    var loginConfig = {
        username: name,
        password: name
    };
    var n = 0;
// 创建一个新的连接

    conn.listen({
        onOpened:
        //    function() {
        //    mui.toast("成功登录");
        //    conn.setPresence();
        //    mui.openWindow({
        //        url: 'html/tab-webview-main.html',
        //        extras:{
        //            username:loginConfig.username.value,
        //            password:loginConfig.password.value
        //        }
        //    })
        //},
            function (message) {          //连接成功回调，连接成功后才可以发送消息
                //如果isAutoLogin设置为false，那么必须手动设置上线，否则无法收消息
                // 手动上线指的是调用conn.setPresence(); 在本例中，conn初始化时已将isAutoLogin设置为true
                // 所以无需调用conn.setPresence();
                console.log("%c [opened] 连接已成功建立", "color: green");
//                      conn.setPresence();   conn isAutoLogin已经是true 不用
                console.log("-----设置上线状态-----")

            },
        onClosed:function(){
            console.log("--连接关闭----")
        },
        //图片   star
        onPictureMessage: function (message) {
            var timer;
            function tesdd(){
                localStorage.clear(minz)
            };

            var div = document.getElementById('content');
            div.scrollTop=div.scrollHeight;
            var minz=message.from; //获取环信ID
            var head_img = message.ext.senderInfo.headImg; //获取头像
            var nick1 = message.ext.senderInfo.nick; //获取昵称
            var useid = message.ext.senderInfo.userId; //获取用户在贵觅里的ID
            var div222; //用于存储 聊天记录

            var dddd = localStorage.getItem(minz);
            if(dddd ==null||dddd==undefined ||dddd==""){dddd=""}else{
                window.clearTimeout(timer);
                timer = setTimeout(tesdd,6000);
            };

            $("#SRZL").find("span").remove();  //  删除消息提示条数
            $("#xiansninz").html(null);
            //message.delay 时间  有时候发过来的消息有 有时没有 不稳定
            if (message.data != "" && $("#mymodal-liaot").css("display") == "none") {
                n++;
            };
            var span ="<span class='text-center' style='position:absolute;top: -5px;right: -5px;display: block;background: red;width: 20px;height: 20px; border-radius: 10px !important; font-size: .5em'>"+n+"</span>"

            $("#SRZL").append(span);   //显示消息提示条数
            $("#SRZL").click(function () {   //在  消息框显示的时候清除消息条数
                $("#SRZL").find("span").remove();
                n = 0 ;
            });
            console.log(message);
            $("#xiansninz").append("<span id='sendId' style='display: none'>"+minz+"</span><span>"+nick1+"</span>");  //左上角显示发给谁
            $xiao1="<div class='text-left'><div ><span>"+nick1+"&nbsp; &nbsp; &nbsp;</span>"+nowtime()+"<br/><img  class='pimg' style='max-width: 200px;max-height: 100px'  src='"+message.url+"'/></div></div>"; //在已经有跟他聊天的情况下 直接添加进去
            $ssss='';
            $ssss="<div class='xiaoxTX'><a href='#"+minz+"' class='btn btn-block' data-toggle='tab'>" +
                "<img style='width: 30px; height: 30px' src='"+head_img+"' alt=''>&nbsp; &nbsp; &nbsp;" +
                ""+nick1+"" +
                "</a>" +
                "<span class='user_id' style='display: none'>"+minz+"</span></div>";
            $xiaoxdiv="<div id="+minz+" class='"+minz+" tab-pane fade' >"+dddd+"<div class='text-left'><div ><span>"+nick1+"&nbsp; &nbsp; &nbsp;</span>"+nowtime()+"<br/><img  class='pimg' style='max-width: 200px;max-height: 100px'  src='"+message.url+"'/></div></div></div>";
            // $("#content").find("div:first-child").addClass("tab-pane fade in active");
            if($(".xiaoxTX").length==0){
                $xiaoxdiv1="<div id="+minz+" class='"+message.from+" tab-pane fade in active'>"+dddd+"<div class='text-left'><div ><span>"+nick1+"&nbsp; &nbsp; &nbsp;</span>"+nowtime()+"<br/><img class='pimg' style='max-width: 200px;max-height: 100px' src='"+message.url+"'/></div></div></div>";
                $("#laotiaolieb").append($ssss);       //创建头像列表
                $("#content").append($xiaoxdiv1); //创建与头像对应的聊天框

                test1();
//              div222 = $("#"+minz).html();
//              localStorage.setItem(minz,div222);
//              setLoca();
                $(".pimg").click(function(){
                    var _this = $(this);//将当前的pimg元素作为_this传入函数
                    imgShow("#outerdiv", "#innerdiv", "#bigimg", _this);
                });
                div.scrollTop=div.scrollHeight;
                return;
            };
            function tru(minz){
                if($("#content").children().hasClass(minz)){
                    return  true;
                }else{
                    return false;
                }
            } ;
            var uidfk=tru(minz);

            for(var j=0;j<$(".xiaoxTX").length;j++){
                if(uidfk){
                    $("."+minz+"").append($xiao1);
//                        $("."+minz+"").css({"dispaly":"block"}).siblings().css({"display":"none"});
                    $("."+minz+"").removeClass("tab-pane fade").addClass("tab-pane fade in active");
                    $("."+minz+"").siblings().removeClass("tab-pane fade in active").addClass("tab-pane fade");
                    // $("#xiansninz").text(minz);
                    test1();
//                  div222 = $("#"+minz).html();
//                  localStorage.setItem(minz,div222);
//                  setLoca();
                    $(".pimg").click(function(){
                        var _this = $(this);//将当前的pimg元素作为_this传入函数
                        imgShow("#outerdiv", "#innerdiv", "#bigimg", _this);
                    });
                    div.scrollTop=div.scrollHeight;
                    return;
                }else{
                    $("#laotiaolieb").append($ssss);       //创建头像列表
                    $("#content").append($xiaoxdiv); //创建与头像对应的聊天框
//                        $("."+minz+"").css({"dispaly":"block"}).siblings().css({"display":"none"});
                    $("."+minz+"").removeClass("tab-pane fade").addClass("tab-pane fade in active");
                    $("."+minz+"").siblings().removeClass("tab-pane fade in active").addClass("tab-pane fade");
                    test1();
//                  div222 = $("#"+minz).html();
//                  localStorage.setItem(minz,div222);
//                  setLoca();
                    $(".pimg").click(function(){
                        var _this = $(this);//将当前的pimg元素作为_this传入函数
                        imgShow("#outerdiv", "#innerdiv", "#bigimg", _this);
                    });
                    div.scrollTop=div.scrollHeight;
                    return;
                }
            };
            $(".pimg").click(function(){
                var _this = $(this);//将当前的pimg元素作为_this传入函数
                imgShow("#outerdiv", "#innerdiv", "#bigimg", _this);
            });
            console.log("Location of Picture is ", message.url);
        },
        //收到图片消息          图片   end
        //收到 文本消息 或 表情消息
        onTextMessage : function(message){
            var timer;
            function tesdd(){
                localStorage.clear(minz)
            };

            var div = document.getElementById('content');
            div.scrollTop=div.scrollHeight;
            var minz=message.from; //获取环信ID
            var head_img = message.ext.senderInfo.headImg; //获取头像
            var nick1 = message.ext.senderInfo.nick; //获取昵称
            var useid = message.ext.senderInfo.userId; //获取用户在贵觅里的ID
            var div222; //用于存储 聊天记录

            var dddd = localStorage.getItem(minz);
            if(dddd ==null||dddd==undefined ||dddd==""){dddd=""}else{
                window.clearTimeout(timer);
                timer = setTimeout(tesdd,6000);
            };

            $("#SRZL").find("span").remove();  //  删除消息提示条数
            $("#xiansninz").html(null);
            //message.delay 时间  有时候发过来的消息有 有时没有 不稳定
            if (message.data != "" && $("#mymodal-liaot").css("display") == "none") {
                n++;
            };
            var span ="<span class='text-center' style='position:absolute;top: -5px;right: -5px;display: block;background: red;width: 20px;height: 20px; border-radius: 10px !important; font-size: .5em'>"+n+"</span>"

            $("#SRZL").append(span);   //显示消息提示条数
            $("#SRZL").click(function () {   //在  消息框显示的时候清除消息条数
                $("#SRZL").find("span").remove();
                n = 0 ;
            });
            console.log(message);
            $("#xiansninz").append("<span id='sendId' style='display: none'>"+minz+"</span><span>"+nick1+"</span>");  //左上角显示发给谁
            $xiao1="<div class='text-left'><div ><span>"+nick1+"&nbsp; &nbsp; &nbsp;</span>"+nowtime()+"<br/>"+message.data+"</div></div>"; //在已经有跟他聊天的情况下 直接添加进去
            $ssss='';
            $ssss="<div class='xiaoxTX'><a href='#"+minz+"' class='btn btn-block' data-toggle='tab'>" +
                "<img style='width: 30px; height: 30px' src='"+head_img+"' alt=''>&nbsp; &nbsp; &nbsp;" +
                ""+nick1+"" +
                "</a>" +
                "<span class='user_id' style='display: none'>"+minz+"</span></div>";
            $xiaoxdiv="<div id="+minz+" class='"+minz+" tab-pane fade' >"+dddd+"<div class='text-left'><div ><span>"+nick1+"&nbsp; &nbsp; &nbsp;</span>"+nowtime()+"<br/>"+message.data+"</div></div></div>";
            // $("#content").find("div:first-child").addClass("tab-pane fade in active");
            if($(".xiaoxTX").length==0){
                $xiaoxdiv1="<div id="+minz+" class='"+message.from+" tab-pane fade in active'>"+dddd+"<div class='text-left'><div ><span>"+nick1+"&nbsp; &nbsp; &nbsp;</span>"+nowtime()+"<br/>"+message.data+"</div></div></div>";
                $("#laotiaolieb").append($ssss);       //创建头像列表
                $("#content").append($xiaoxdiv1); //创建与头像对应的聊天框

                test1();
//              div222 = $("#"+minz).html();
//              localStorage.setItem(minz,div222);
                 setLoca();
                div.scrollTop=div.scrollHeight;
                return;
            };
            function tru(minz){
                if($("#content").children().hasClass(minz)){
                    return  true;
                }else{
                    return false;
                }
            } ;
            var uidfk=tru(minz);

            for(var j=0;j<$(".xiaoxTX").length;j++){
                if(uidfk){
                    $("."+minz+"").append($xiao1);
//                        $("."+minz+"").css({"dispaly":"block"}).siblings().css({"display":"none"});
                    $("."+minz+"").removeClass("tab-pane fade").addClass("tab-pane fade in active");
                    $("."+minz+"").siblings().removeClass("tab-pane fade in active").addClass("tab-pane fade");
                    // $("#xiansninz").text(minz);
                    test1();
//                  div222 = $("#"+minz).html();
//                  localStorage.setItem(minz,div222);
setLoca();
                    div.scrollTop=div.scrollHeight;
                    return;
                }else{
                    $("#laotiaolieb").append($ssss);       //创建头像列表
                    $("#content").append($xiaoxdiv); //创建与头像对应的聊天框
//                        $("."+minz+"").css({"dispaly":"block"}).siblings().css({"display":"none"});
                    $("."+minz+"").removeClass("tab-pane fade").addClass("tab-pane fade in active");
                    $("."+minz+"").siblings().removeClass("tab-pane fade in active").addClass("tab-pane fade");
                    test1();
//                  div222 = $("#"+minz).html();
//                  localStorage.setItem(minz,div222);
setLoca();
                    div.scrollTop=div.scrollHeight;
                    return;
                }
            };

        },
        onEmojiMessage: function (message) {
            // 当为WebIM添加了Emoji属性后，若发送的消息含WebIM.Emoji里特定的字符串，connection就会自动将
            // 这些字符串和其它文字按顺序组合成一个数组，每一个数组元素的结构为{type: 'emoji(或者txt)', data:''}
            // 当type='emoji'时，data表示表情图像的路径，当type='txt'时，data表示文本消息
            var data = message.data;
            var ddddddd1="",c;
            $addconteny =[]; $addconteny1 =[];
            for(var i = 0 , l = data.length ; i < l ; i++){
                console.log(data[i].type);
                if(data[i].type == "txt"){
                    $addconteny[i]  = "<span>"+data[i].data+"</span>";

                }
                if(data[i].type == "emoji"){
                    $addconteny[i] = "<img src='"+data[i].data+"'/>";

                }

            }
            ddddddd1= $addconteny;
            c= ddddddd1.join("");
            var timer;
            function tesdd(){
                localStorage.clear(minz)
            };

            var div = document.getElementById('content');
            div.scrollTop=div.scrollHeight;
            var minz=message.from; //获取环信ID
            var head_img = message.ext.senderInfo.headImg; //获取头像
            var nick1 = message.ext.senderInfo.nick; //获取昵称
            var useid = message.ext.senderInfo.userId; //获取用户在贵觅里的ID
            var div222; //用于存储 聊天记录

            var dddd = localStorage.getItem(minz);
            if(dddd ==null||dddd==undefined ||dddd==""){dddd=""}else{
                window.clearTimeout(timer);
                timer = setTimeout(tesdd,6000);
            };

            $("#SRZL").find("span").remove();  //  删除消息提示条数
            $("#xiansninz").html(null);
            //message.delay 时间  有时候发过来的消息有 有时没有 不稳定
            if (message.data != "" && $("#mymodal-liaot").css("display") == "none") {
                n++;
            };
            var span ="<span class='text-center' style='position:absolute;top: -5px;right: -5px;display: block;background: red;width: 20px;height: 20px; border-radius: 10px !important; font-size: .5em'>"+n+"</span>"

            $("#SRZL").append(span);   //显示消息提示条数
            $("#SRZL").click(function () {   //在  消息框显示的时候清除消息条数
                $("#SRZL").find("span").remove();
                n = 0 ;
            });
            console.log(message);
            $("#xiansninz").append("<span id='sendId' style='display: none'>"+minz+"</span><span>"+nick1+"</span>");  //左上角显示发给谁
            $xiao1="<div class='text-left'><div ><span>"+nick1+"&nbsp; &nbsp; &nbsp;</span>"+nowtime()+"<br/>"+c+"</div></div>"; //在已经有跟他聊天的情况下 直接添加进去
            $ssss='';
            $ssss="<div class='xiaoxTX'><a href='#"+minz+"' class='btn btn-block' data-toggle='tab'>" +
                "<img style='width: 30px; height: 30px' src='"+head_img+"' alt=''>&nbsp; &nbsp; &nbsp;" +
                ""+nick1+"" +
                "</a>" +
                "<span class='user_id' style='display: none'>"+minz+"</span></div>";
            $xiaoxdiv="<div id="+minz+" class='"+minz+" tab-pane fade' >"+dddd+"<div class='text-left'><div ><span>"+nick1+"&nbsp; &nbsp; &nbsp;</span>"+nowtime()+"<br/>"+c+"</div></div></div>";
            // $("#content").find("div:first-child").addClass("tab-pane fade in active");
            if($(".xiaoxTX").length==0){
                $xiaoxdiv1="<div id="+minz+" class='"+message.from+" tab-pane fade in active'>"+dddd+"<div class='text-left'><div ><span>"+nick1+"&nbsp; &nbsp; &nbsp;</span>"+nowtime()+"<br/>"+c+"</div></div></div>";
                $("#laotiaolieb").append($ssss);       //创建头像列表
                $("#content").append($xiaoxdiv1); //创建与头像对应的聊天框

                test1();
//              div222 = $("#"+minz).html();
//              localStorage.setItem(minz,div222);
setLoca();
                div.scrollTop=div.scrollHeight;
                return;
            };
            function tru(minz){
                if($("#content").children().hasClass(minz)){
                    return  true;
                }else{
                    return false;
                }
            } ;
            var uidfk=tru(minz);

            for(var j=0;j<$(".xiaoxTX").length;j++){
                if(uidfk){
                    $("."+minz+"").append($xiao1);
//                        $("."+minz+"").css({"dispaly":"block"}).siblings().css({"display":"none"});
                    $("."+minz+"").removeClass("tab-pane fade").addClass("tab-pane fade in active");
                    $("."+minz+"").siblings().removeClass("tab-pane fade in active").addClass("tab-pane fade");
                    // $("#xiansninz").text(minz);
                    test1();
//                  div222 = $("#"+minz).html();
//                  localStorage.setItem(minz,div222);
setLoca();
                    div.scrollTop=div.scrollHeight;
                    return;
                }else{
                    $("#laotiaolieb").append($ssss);       //创建头像列表
                    $("#content").append($xiaoxdiv); //创建与头像对应的聊天框
//                        $("."+minz+"").css({"dispaly":"block"}).siblings().css({"display":"none"});
                    $("."+minz+"").removeClass("tab-pane fade").addClass("tab-pane fade in active");
                    $("."+minz+"").siblings().removeClass("tab-pane fade in active").addClass("tab-pane fade");
                    test1();
//                  div222 = $("#"+minz).html();
//                  localStorage.setItem(minz,div222);
setLoca();
                    div.scrollTop=div.scrollHeight;
                    return;
                }
            };
        },   //收到表情消息

        //收到音频消息 star
        onAudioMessage: function ( message ) {
            var options = { url: message.url };
            console.log(message.url)
            options.onFileDownloadComplete = function ( response ) {
                //音频下载成功，需要将response转换成blob，使用objectURL作为audio标签的src即可播放。
                var objectURL = WebIM.utils.parseDownloadResponse.call(conn, response);
                console.log(objectURL)
                console.log(options.url+"."+options.headers.Accept)
                var timer;
                function tesdd(){
                    localStorage.clear(minz)
                };
                var div = document.getElementById('content');
                div.scrollTop=div.scrollHeight;
                var minz=message.from; //获取环信ID
                var head_img = message.ext.senderInfo.headImg; //获取头像
                var nick1 = message.ext.senderInfo.nick; //获取昵称
                var useid = message.ext.senderInfo.userId; //获取用户在贵觅里的ID
                var div222; //用于存储 聊天记录

                var dddd = localStorage.getItem(minz);
                if(dddd ==null||dddd==undefined ||dddd==""){dddd=""}else{
                    window.clearTimeout(timer);
                    timer = setTimeout(tesdd,6000);
                };

                $("#SRZL").find("span").remove();  //  删除消息提示条数
                $("#xiansninz").html(null);
                //message.delay 时间  有时候发过来的消息有 有时没有 不稳定
                if (message.data != "" && $("#mymodal-liaot").css("display") == "none") {
                    n++;
                };
                var span ="<span class='text-center' style='position:absolute;top: -5px;right: -5px;display: block;background: red;width: 20px;height: 20px; border-radius: 10px !important; font-size: .5em'>"+n+"</span>"

                $("#SRZL").append(span);   //显示消息提示条数
                $("#SRZL").click(function () {   //在  消息框显示的时候清除消息条数
                    $("#SRZL").find("span").remove();
                    n = 0 ;
                });
                console.log(message);
                $("#xiansninz").append("<span id='sendId' style='display: none'>"+minz+"</span><span>"+nick1+"</span>");  //左上角显示发给谁
                $xiao1="<div class='text-left'><div ><span>"+nick1+"&nbsp; &nbsp; &nbsp;</span>"+nowtime()+"<br/><audio controls='controls'>"+
                    "<source src='"+objectURL+"' type='audio/ogg'>"+
                    "<source src='"+objectURL+"' type='audio/mpeg'>"+
                    "Your browser does not support the audio element."+
                    "</audio></div></div>"; //在已经有跟他聊天的情况下 直接添加进去
                $ssss='';
                $ssss="<div class='xiaoxTX'><a href='#"+minz+"' class='btn btn-block' data-toggle='tab'>" +
                    "<img style='width: 30px; height: 30px' src='"+head_img+"' alt=''>&nbsp; &nbsp; &nbsp;" +
                    ""+nick1+"" +
                    "</a>" +
                    "<span class='user_id' style='display: none'>"+minz+"</span></div>";
                $xiaoxdiv="<div id="+minz+" class='"+minz+" tab-pane fade' >"+dddd+"<div class='text-left'><div ><span>"+nick1+"&nbsp; &nbsp; &nbsp;</span>"+nowtime()+"<br/><audio controls='controls'>"+
                    "<source src='"+objectURL+"' type='audio/ogg'>"+
                    "<source src='"+objectURL+"' type='audio/mpeg'>"+
                    "Your browser does not support the audio element."+
                    "</audio></div></div></div>";
                // $("#content").find("div:first-child").addClass("tab-pane fade in active");
                if($(".xiaoxTX").length==0){
                    $xiaoxdiv1="<div id="+minz+" class='"+message.from+" tab-pane fade in active'>"+dddd+"<div class='text-left'><div ><span>"+nick1+"&nbsp; &nbsp; &nbsp;</span>"+nowtime()+"<br/><audio controls='controls'>"+
                        "<source src='"+objectURL+"' type='audio/ogg'>"+
                        "<source src='"+objectURL+"' type='audio/mpeg'>"+
                        "Your browser does not support the audio element."+
                        "</audio></div></div></div>";
                    $("#laotiaolieb").append($ssss);       //创建头像列表
                    $("#content").append($xiaoxdiv1); //创建与头像对应的聊天框

                    test1();
//                  div222 = $("#"+minz).html();
//                  localStorage.setItem(minz,div222);
                    div.scrollTop=div.scrollHeight;
                    setLoca();
                    return;
                };
                function tru(minz){
                    if($("#content").children().hasClass(minz)){
                        return  true;
                    }else{
                        return false;
                    }
                } ;
                var uidfk=tru(minz);

                for(var j=0;j<$(".xiaoxTX").length;j++){
                    if(uidfk){
                        $("."+minz+"").append($xiao1);
//                        $("."+minz+"").css({"dispaly":"block"}).siblings().css({"display":"none"});
                        $("."+minz+"").removeClass("tab-pane fade").addClass("tab-pane fade in active");
                        $("."+minz+"").siblings().removeClass("tab-pane fade in active").addClass("tab-pane fade");
                        // $("#xiansninz").text(minz);
                        test1();
//                      div222 = $("#"+minz).html();
//                      localStorage.setItem(minz,div222);
setLoca();
                        div.scrollTop=div.scrollHeight;
                        return;
                    }else{
                        $("#laotiaolieb").append($ssss);       //创建头像列表
                        $("#content").append($xiaoxdiv); //创建与头像对应的聊天框
//                        $("."+minz+"").css({"dispaly":"block"}).siblings().css({"display":"none"});
                        $("."+minz+"").removeClass("tab-pane fade").addClass("tab-pane fade in active");
                        $("."+minz+"").siblings().removeClass("tab-pane fade in active").addClass("tab-pane fade");
                        test1();
//                      div222 = $("#"+minz).html();
//                      localStorage.setItem(minz,div222);
setLoca();
                        div.scrollTop=div.scrollHeight;
                        return;
                    }
                }
            };

            options.onFileDownloadError = function () {
                //音频下载失败
            };

            //通知服务器将音频转为mp3
            options.headers = {
                'Accept': 'audio/mp3'
            };

            WebIM.utils.download.call(conn, options);


        },
        //收到音频消息 end
        onError : function(message){
//                    handleError(message);
            console.log(message)
            if (message.type == 8) {
            	alert("你已下线！！")
            }
        },
        onEmotionMessage : function(message) {

            handleEmotion(message);

        },
    });

    //一对一发送消息

    // $("#content").show().next().hide();
    // $("#laotiaolieb").show().next().hide();

   var username=$(".username").text();
    var options = {
        apiUrl:WebIM.config.apiURL,
        user: "service_account",
        pwd: "gmkj123456",
        appKey: WebIM.config.appkey
    };
    conn.open(options);
    $("#private_chat").click(function(){
        $(".sendContent").show().next().hide();
        $("#xiansninz_1").hide().prev().show();
        $("#content_qun").hide().prev().show();
        $("#qunsend_1").css({"display":"none"});
        $("#qunsend").css({"display":"block"});
        $("#laotiaolieb").css({"display":"block"});
        $("#qunleibiao").css({"display":"none"});

    });
//  $("#suggest_table").ajaxComplete(function() { //意思是在重新加载完了之后再执行函数
//  $(".xiaoxi-fasong").click(function(){
//  	
//      $(".sendContent").show().next().hide();
//      $("#xiansninz_1").hide().prev().show();
//      $("#content_qun").hide().prev().show();
//      $("#qunsend_1").css({"display":"none"});
//      $("#qunsend").css({"display":"block"});
//      $("#laotiaolieb").css({"display":"block"});
//      $("#qunleibiao").css({"display":"none"});
//  })
//  })
});

var sendPrivateText = function () {
    var id = $("#sendContent").val();                 // 生成本地消息id
    var msg = new WebIM.message('txt', id);      // 创建文本消息
    var iiid=($("#sendId").text());
    msg.set({
        msg: id,                  // 消息内容
        to: iiid,                          // 接收消息对象（用户id）
        roomType: false,
        success: function (id, serverMsgId) {
            console.log(iiid);    //id值的是消息文本内容 serverMsgId 消息内容的ID
//          var div222 = $("#"+iiid).html();
//          localStorage.setItem(iiid,div222);
            var div = document.getElementById('content');

            div.scrollTop=div.scrollHeight;
        }
    });

    msg.body.chatType = 'singleChat';
    conn.send(msg.body);
    $str="<div class='text-right'><div class='rightdiv'>"+nowtime()+"&nbsp; &nbsp; &nbsp;"+conn.context.userId+"<br/>"+$("#sendContent").val()+"</div></div>"
    $("."+iiid+"").append($str);
    setLoca();
};
function sendMsg(){
    if($("#sendContent").val()==""){
        return;
    }
    sendPrivateText();
    $("#sendContent").val("");

};
$("#sendContent").val("");
$("#sendContent").keydown(function(e){
    if(e.keyCode==13){
        if($("#sendContent").val()==""){
            $("#sendContent").val("");
        }else{
            sendPrivateText();
            $("#sendContent").val("");
        }
    }
});

function test1(){      //点击头像改变发送对象
    $(".xiaoxTX").click(function(){
        $("#xiansninz").html(null);
        var lk=  $(this).children("a").text();
        var  ureid= $(this).find("span").text();

//                alert(lk)
        $("#xiansninz").append("<span id='sendId' style='display: none'>"+ureid+"</span><span>"+lk+"</span>");
    })
};

//点击发送消息 获取用户的ID 并且创建头像和聊天框
$("#suggest_table").ajaxComplete(function() {
		$(".xiaoxi-fasong").unbind("click").on('click',function(){
      
			$("#xiansninz").html(null);
			var huoquId = $(this).find("span").text();
			var huiImg = $(this).find("img").attr("src"); //暂时没图片
			var huiNick = $(this).find("small").text();
			$("#xiansninz").append("<span id='sendId' style='display: none'>" + huoquId + "</span><span>&nbsp; &nbsp; &nbsp;" + huiNick + "</span>");
			var tttt = "<div class='xiaoxTX'><a href='#" + huoquId + "' class='btn btn-block' data-toggle='tab'>" +
				"<img style='width: 30px; height: 30px' src='" + huiImg + "' alt=''>&nbsp; &nbsp; &nbsp;" +
				"" + huiNick + "" +
				"</a>" +
				"<span class='user_id' style='display: none'>" + huoquId + "</span></div>";
			var xiaodiv2 = "<div id=" + huoquId + " class='" + huoquId + " tab-pane fade in active'></div>";
			if($(".xiaoxTX").length == 0) {
				$("#laotiaolieb").append(tttt); //创建头像列表
				$("#content").append(xiaodiv2); //创建与头像对应的聊天框
				test1();
			}
			if($("#content div").hasClass(huoquId)) {
				$("." + huoquId + "").removeClass("tab-pane fade").addClass("tab-pane fade  in active");
				$("." + huoquId + "").siblings().removeClass("tab-pane fade in active").addClass("tab-pane fade");
				test1();
			} else {
				$("#laotiaolieb").append(tttt); //创建头像列表
				$("#content").append(xiaodiv2); //创建与头像对应的聊天框
				$("." + huoquId + "").siblings().removeClass("tab-pane fade in active").addClass("tab-pane fade");
				test1();
			}
		})
})
//时间格式化
function nowtime(){//将当前时间转换成yyyymmdd格式
  function p(s) {
    return s < 10 ? '0' + s: s;
}

var myDate = new Date();
//获取当前年
var year=myDate.getFullYear();
//获取当前月
var month=myDate.getMonth()+1;
//获取当前日
var date=myDate.getDate(); 
var h=myDate.getHours();       //获取当前小时数(0-23)
var m=myDate.getMinutes();     //获取当前分钟数(0-59)
var s=myDate.getSeconds();  

var now=year+'-'+p(month)+"-"+p(date)+" "+p(h)+':'+p(m)+":"+p(s);
return now
};
//消息模板浏览
$("#myModalxiaoxioo").click(function () {
    $.ajax({
        type: "POST",
        url: "xiaoxmoban.json",
        dataType: "json",
        success: function (data) {
            var xiaox = data.suhju.length;
            var div = "";
            for (var i = 0; i < xiaox; i++) {
                div += "<div class='moban-content' style='float: left;width: 40%;margin-left: 5%;border: 1px solid lightgrey;height: 120px;margin-top: 10px;overflow: auto;'><h5 style='font-weight: 600' class='text-center'>" + data.suhju[i].titile + "</h5><div>" + data.suhju[i].content + "</div></div>";

            }
            $("#moban").html(div);
            $(".moban-content").click(function () {
                $(this).css({"border": "1px solid"}).siblings().css("border", "1px solid lightgrey");
            });
            $(".moban-content").dblclick(function () {
                var getconten = $(this).text();     //双击搜索
                $("#sendContent").val(getconten);
                $("#myModalxiaoxi").modal("hide");
                $("#sendContent").focus()
            });
        }
    })
});
//$('#mymodal-liaot').on('shown.bs.modal', function () {
//  var chatContent = $("#mymodal-liaot").html();
//  localStorage.setItem("Chat",chatContent);
//})
//$('#mymodal-liaot').on('hidden.bs.modal', function () {
//  var chatContent = $("#mymodal-liaot").html();
//  localStorage.setItem("Chat",chatContent);
//})